import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

import Layout from "@/views/layout/Layout";
import { getCookie } from '@/utils/cookie';

const r = new Router({
    // mode: "history", 关闭 history
    routes: [
        {
            path: '',
            redirect: '/info',
        }, {
            path: '/display',
            component: Layout,
            children: [
                {
                    path: '',
                    name: 'display',
                    component: () => import('@/views/display/Display'),
                    meta: { title: '展示', icon: '' }
                }
            ]
        }, {
            path: '/info',
            component: Layout,
            children: [
                {
                    path: 'update',
                    name: 'update',
                    component: () => import('@/views/info/update'),
                    meta: { title: '编辑', icon: '' }
                }, {
                    path: 'add',
                    name: 'add',
                    component: () => import('@/views/info/add'),
                    meta: { title: '添加', icon: '' }
                }, {
                    path: '',
                    name: 'info',
                    component: () => import('@/views/info/index'),
                    meta: { title: '查看', icon: '' }
                }
            ]
        }, {
            path: '/login',
            name: 'login',
            component: () => import('@/views/login/login'),
        }
    ]
})

r.beforeEach((to, from, next) => {
    // ...
    // alert('ya')
    // var isLogin = localStorage.getItem('isLogin')
    var isLogin = getCookie('isLogin');
    // alert(isLogin)
    // console.log('isLogin = ', isLogin)
    if (isLogin == 'true') {
        if (to.path === '/login') { r.push("/info") }
        else{
            next()
        }
    }
    else {
        // console.log(to.path);
        // this.$message.info('登录过期，请重新登录')
        if (to.path !== '/login') { r.push("/login") }
        else{
            next()
        }
        // console.log(r);
        // alert('ba')
    }
})

export default r
